import COS from 'cos-js-sdk-v5'

const Bucket = ''; /* 存储桶 */
const Region = 'ap-guangzhou';
const cos = new COS({
    SecretId: '',
    SecretKey: '',
});
export const upload = {
    //上传
    uploadChange(fileData, url) {

        const files = {}
        files.Body = fileData.file
        files.Bucket = Bucket
        // files.Bucket = Bucket
        files.Key = url + '/' + fileData.file.name
        files.Region = Region
        let arrList = []
        arrList.push(files)

        cos.uploadFiles({
            files: arrList,
            SliceSize: 1024 * 1024 * 10,
            /* 设置大于10MB采用分块上传 */
            onProgress: function (info) {
                var percent = parseInt(info.percent * 10000) / 100;
                var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;

            },
            onFileFinish: function (err, data, options) {

            },
        }, (err, data) => {
            if (err) {
                console.log('上传失败', err);
                return;
            }

        });
        return arrList
    },
    //下载
    DownloadFile(filename) {
        cos.getObjectUrl({
            Bucket,
            /* 必须 */
            Region,
            /* 存储桶所在地域，必须字段 */
            Key: 'file/' + filename,
            /* 必须 */
        }, function (err, data) {
            if (err) {
                console.log(err);
                return;
            }
            const url = data.Url + (data.Url.indexOf('?') > -1 ? '&' : '?') + 'response-content-disposition=attachment'; // 补充强制下载的参数
            // 使用iframe下载
            const elemIF = document.createElement("iframe");
            elemIF.src = url;
            elemIF.style.display = "none";
            document.body.appendChild(elemIF);
        });
    },
    uploadChangeHeard(fileData, url) {

        const files = {}
        files.Body = fileData
        files.Bucket = Bucket
        // files.Bucket = Bucket
        files.Key = url + '/' + fileData.name
        files.Region = Region
        let arrList = []
        arrList.push(files)

        cos.uploadFiles({
            files: arrList,
            SliceSize: 1024 * 1024 * 10,
            /* 设置大于10MB采用分块上传 */
            onProgress: function (info) {
                var percent = parseInt(info.percent * 10000) / 100;
                var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;

            },
            onFileFinish: function (err, data, options) {

            },
        }, (err, data) => {
            if (err) {
                console.log('上传失败', err);
                return;
            }

        });
        return arrList
    },
}